$(function () {
    lazyUserCenterImage();
    queryMyGiftCardData(1);
    var ends = $('.endTime');
    $.each(ends,function(index,end){
        var endTime = $(end).data('endtime');
        var date = new Date(endTime);
        if(new Date() > date){
            var id = $(end).data('id');
            var text = $('button[data-id='+id+']').text();
            if(text == '激 活'){
                $('button[data-id='+id+']').html("已过期").attr('disabled','disabled');
            }
        }
    });

    $('body').on('click','.activation',function () {//ID激活
        var _this = $(this);
        var id = _this.data('id');
        activationGiftCard(id,null,null,_this);
    }).on('click','#manual-activation',function () {//手动激活
        var context = $("#activate-gift-cart-div-box").html();
        icModale(2, '<strong>激活礼卡</strong>', context, '确认', '520px', function(index, ele) {
            var code = ele.find("#code").val();
            var codePwd = ele.find("#codePwd").val();
            activationGiftCard(0,code,codePwd);
            parent.layer.close(index);
        });
    });
    function queryMyGiftCardData(pageNo) {
        var params="{}";
        getPageAjaxData({
            url: baseloaction + '/web/uc/op/ajax/myAjaxGiftCard',//请求URL
            contextId:'giftCard_list_context',//响应内容页面设置内容ID
            params:params,//请求参数
            pageNo:pageNo,//页码
            pageCall:queryMyGiftCardData, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId:'11', //全选框ID
            callback:function () {//数据加载完成回调

            }
        });
    }

    /**
     * 激活礼卡
     * @param id 礼卡ID
     * @param code 礼卡编号
     * @param codePwd 礼卡密码
     */
    function activationGiftCard(id,code,codePwd,em) {
        $.ajax({
            url:baseloaction +'/web/uc/op/activateGiftCardCode',
            type:'post',
            dataType:'json',
            async:false,
            data:{'id':id,'code':code,'codePwd':codePwd},
            success:function (result) {
                icModale(0,'',result.message,null,null,null);
                if(result.success==true){
                    if(id!=0){
                        $(em).removeClass("activation");
                        $(em).html("已激活").attr('disabled', true);
                    }
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，礼卡激活失败',null,null,null);
            }
        });
    }
});


/**
 * 加载课程图片
 */
function lazyUserCenterImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        placeholder: staticPath+'/static/front/web/v3/img/default/default-face.gif',  //占位图
        effect: 'fadeIn' //载入方式
    });
}